package com.example.demo.controller;

import com.example.demo.mapper.UserMapper;
import com.example.demo.entities.User;
import com.example.demo.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import java.util.List;

@Slf4j
@RestController
public class InsertController {
    Logger logger = LoggerFactory.getLogger(getClass());

    /**
     * 所有的外部类的注入，都统一放在上面，方法放在下面
     * controller 调用 service ，service调用mapper
     */

    @Autowired
    UserService userService;

    /**
     * controller不要调用mapper
     */
    @Resource
    UserMapper userMapper;

    @PostMapping("/insert")
    public String add(String username, String password) {

        String result = userService.insert(username, password);

        return result;
    }

    @PostMapping("/select")
    public User select(Integer id) {

        logger.info("查询" + id + "的详细信息");
        logger.info("我在学习git呀");
        User user = userMapper.getUserById(id);

        return user;
    }

    @PostMapping("/selectusers")
    public List<User> selectusers(@RequestBody List<Integer> ids, @RequestBody List<String> username) {

        logger.info("查询:{}的详细s信息", ids);

        return userMapper.getUsers(ids);

    }

    @GetMapping("/update")
    public String update(User user) {
        logger.info("查询:{}的详细s信息", user);
        userMapper.updateUser(user);
        return "用户" + user.getId() + "信息更新完成";
    }

    @DeleteMapping("/delete")
    public String delect(Integer id) {
        userMapper.deleteUserById(id);
        return "删除用户" + id;
    }

    @PostMapping("/queryUsers")
    public List<User> query(User user) {
        List result = userMapper.queryUsers(user);
        return result;
    }

    @PostMapping("/login")
    public boolean login(String password, String username,Integer id,HttpServletRequest req) {
        User user = userMapper.getUserById(id);
        if (user!=null){

            HttpSession session=req.getSession();//创建session对象
            //把用户数据保存在session域对象中
            session.setAttribute("loginUser", user);
            String pwd = user.getPassword();
            String uname = user.getUsername();
            if (pwd.equals(password) && uname.equals(username)) {
                return true;
            } else{
                return false;
            }
        }

        return false;
    }
}
